#include <QtGui/QMouseEvent>
#include <QtOpenGL>
#include <QyDebug.h>
#include "QyGLSettings.h"
#include "QyGLViewWidget.h"
#include "3rdparty/glextensions.h"

using namespace QyGL;

/**************************************************************************************************/

ViewSettings::ViewSettings() :
    resolution( 640, 480 ),
    format( QGLFormat::defaultFormat() ),
    updateRate(30)
{

    projection.fov = 45.0;
    projection.aspect = 4/3;
    projection.zNear = 0.1;
    projection.zFar = 10000.0;
    projection.adjustAspect = true;

}

/**************************************************************************************************/

ViewWidget::ViewWidget( QWidget * parent ) : QGraphicsView(parent) {

    init();

}

ViewWidget::ViewWidget( const ViewSettings & viewSettings, QWidget * parent )
    : QGraphicsView(parent), p_viewSettings(viewSettings)
{

    init();

}

ViewWidget::~ViewWidget() {

}

QGLWidget * ViewWidget::glWidget() const {

    return p_glWidget;
}

void ViewWidget::init() {

    p_glWidget = new QGLWidget( p_viewSettings.format, this );
    setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
    setMouseTracking(true);
    p_glWidget->setMouseTracking(true);
    setViewport(p_glWidget);

    QGLExtensionFunctions & exts = getGLExtensionFunctions();
    exts.resolve( p_glWidget->context() );
    resize(p_viewSettings.resolution);

}

void ViewWidget::resizeEvent( QResizeEvent * event ) {

    if ( scene() )
        scene()->setSceneRect( QRect( QPoint( 0, 0 ), event->size() ) );

    QGraphicsView::resizeEvent(event);

}
